<?php

class UnionAdmin extends AdminBase
{
	private $nohf = ['uniondel'];

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header('admin');
		}
		$this->mysql = n_api('mysql')->cname('weixin');
		// $this->api = n_api('union');
	}

	public function unionList()
	{
		$param = $this->param('page:char:ignore');
		if (isset($param['page']))
			$page = $param['page'];
		else
			$page = 1;

		$perpage = 50;
		$weixin = n_api('weixin');

		$count = $weixin->getWeixinCount();
		$skip = ($page - 1) * $perpage;
		
		$rs = $weixin->getWeixinList($skip, $perpage,1,1);
		$weixinList = [];
		foreach ($rs as $value) {
			$wxidArrAll[] = $value['wxid'];
			$weixinList[$value['wxid']] = $value;
		}

		$query = "select * from union_main where isdel = 'N'";
		$rs = $this->mysql->select($query);

		foreach ($rs as $value) {
			$ppidArr[] = $value['ppid'];
			$wxidUser[$value['wxid']] = $value['ppid'];
		}

		$ppidArr = array_unique($ppidArr);

		$userInfoArr = n_api('passport')->get($ppidArr);
		
		foreach ($userInfoArr as $userInfo) {
			$user[$userInfo['id']] = $userInfo;
		}

		$unionInfo = [];
		foreach ($weixinList as $wxInfo) {
			$wxid = $wxInfo['wxid'];
			$tmp['wxInfo'] = $wxInfo;
			$tmp['userInfo'] = isset($user[$wxidUser[$wxid]]) ? $user[$wxidUser[$wxid]] : '';
			$unionInfo[] = $tmp;
		}


		$data['unionInfo'] = $unionInfo;
		$data['paging'] = n_api('common')->paging(n_url('union', 'unionList'), $page, $count, $perpage);
		$this->_html('unionList', $data);
	}

	public function unionDel()
	{
		$param = $this->param('wxid:char:empty');
		$wxid = $param['wxid'];
		$ppid = $_SESSION['ppid'];

		$query = "update union_main set isdel = 'Y' where wxid = '$wxid' and ppid = $ppid";
		$this->mysql->exec($query);
		echo "ok";
	}

	public function unionAdd() {
		$groups = n_api('weixin')->getCategories();
		foreach($groups as $key=>$val){
			$data['groups'][$val['first_category']][$val['id']] = $val;
		}
		$this->_html('unionAdd', $data);
	}

	public function doAdd() {
		$param = $this->param('url:char:empty,cid:char:empty');
		
		$weixinUrlLists = preg_replace('/\s{2,}/ui', "\n", trim($param['url']));
		if (strlen($weixinUrlLists) == 0) {
			n_mod("skin")->save("url不能为空");
			exit();
		}
		$weixinArr = explode("\n", $weixinUrlLists);
		n_api('weixin')->addWx($weixinArr);
		
		n_mod("skin")->save("添加成功");
		exit();		
		
		
		
		
		
		$url = $param["url"];
		$groupId = $param["cid"];

		$urlQuery = parse_url($url, PHP_URL_QUERY);
		parse_str($urlQuery);

		$weixin = n_api('weixin');
		$rs = $weixin->getArticleByUrl($url);
		$weixinInfo = $weixin->getArticleWeixinInfo($rs);
		
		$wxid = $weixinInfo['wxid'];

		if (!$weixin->checkExits($wxid) and $wxid != "") {
			$wxname = $weixinInfo['nickname'];
			$desc = $weixinInfo['desc'];
			$face_url = $weixinInfo["face_url"];

			$weixin->addWeixin($wxid, $wxname, $desc, $groupId, $__biz, $face_url);
			echo 'add successfully';
		} else {
			echo 'added already';
		}
	}

	public function __destruct(){
		//加入标准尾。
		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->footer();
		}
	}
}